'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const i = require('../nodes/identity.cjs'),
  l = require('../visit.cjs');
function d(o) {
  if (/[\x00-\x19\s,[\]{}]/.test(o)) {
    const r = `Anchor must not contain whitespace or control characters: ${JSON.stringify(
      o,
    )}`;
    throw new Error(r);
  }
  return !0;
}
function h(o) {
  const t = new Set();
  return (
    l.visit(o, {
      Value(r, n) {
        n.anchor && t.add(n.anchor);
      },
    }),
    t
  );
}
function u(o, t) {
  for (let r = 1; ; ++r) {
    const n = `${o}${r}`;
    if (!t.has(n)) return n;
  }
}
function f(o, t) {
  const r = [],
    n = new Map();
  let c = null;
  return {
    onAnchor: s => {
      r.push(s), c || (c = h(o));
      const e = u(t, c);
      return c.add(e), e;
    },
    setAnchors: () => {
      for (const s of r) {
        const e = n.get(s);
        if (
          typeof e == 'object' &&
          e.anchor &&
          (i.isScalar(e.node) || i.isCollection(e.node))
        )
          e.node.anchor = e.anchor;
        else {
          const a = new Error(
            'Failed to resolve repeated object (this should not happen)',
          );
          throw ((a.source = s), a);
        }
      }
    },
    sourceObjects: n,
  };
}
exports.anchorIsValid = d;
exports.anchorNames = h;
exports.createNodeAnchors = f;
exports.findNewAnchor = u;
